package com.healthcloud.service.impl;

import com.healthcloud.dao.losewisdom.LoseWisdomSingleChoiceAnswerDao;
import com.healthcloud.dao.losewisdom.LoseWisdomSingleChoiceAnswerDetailDao;
import com.healthcloud.dto.req.losewisdom.LoseWisdomSingleChoiceAnswerReq;
import com.healthcloud.dto.req.losewisdom.LoseWisdomSingleChoiceSaveReq;
import com.healthcloud.dto.vo.LoseWisdomSingleChoiceVo;
import com.healthcloud.entity.losewisdom.LoseWisdomSingleChoiceAnswerDetailEntity;
import com.healthcloud.entity.losewisdom.LoseWisdomSingleChoiceAnswerEntity;
import com.healthcloud.service.losewisdom.LoseWisdomSingleChoiceAnswerService;
import com.healthcloud.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;


@Deprecated
@Service("loseWisdomSingleChoiceAnswerService")
public class LoseWisdomSingleChoiceAnswerServiceImpl implements LoseWisdomSingleChoiceAnswerService {

	@Autowired
	private LoseWisdomSingleChoiceAnswerDao loseWisdomSingleChoiceAnswerDao;

	@Autowired
	private LoseWisdomSingleChoiceAnswerDetailDao loseWisdomSingleChoiceAnswerDetailDao;

	/**
	 * 获取问卷答案列表
	 * @param req
	 * @return
	 */
	@Override
	public List<LoseWisdomSingleChoiceAnswerEntity> queryList(LoseWisdomSingleChoiceAnswerReq req) {
		return loseWisdomSingleChoiceAnswerDao.queryList(req);
	}

	/**
	 * 保存/修改筛查问卷答案
	 * @param req
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdateAnswer(LoseWisdomSingleChoiceSaveReq req) {
		if(0 == req.getLoseWisdomSingleChoiceVoList().size()) {
			return;
		}

		Integer phqScore = 0;
		Integer gadScore = 0;
		//1、计算分数
		List<LoseWisdomSingleChoiceVo> answerList = req.getLoseWisdomSingleChoiceVoList();
		for(LoseWisdomSingleChoiceVo vo : answerList) {
			if("PHQ-9".equals(vo.getQuestionTitle())) {
				phqScore += calculateAnswerScore(vo.getAnswer());
			} else if("GAD-7".equals(vo.getQuestionTitle())) {
				gadScore += calculateAnswerScore(vo.getAnswer());
			}
		}

		//2、判断筛查结果
		String phqResult = decidePhqResult(phqScore);
		String gadResult = decideGadResult(gadScore);

		//3、在【lose_wisdom_single_choice_answer】表中存入/修改筛查结果
		LoseWisdomSingleChoiceAnswerEntity answerEntity = new LoseWisdomSingleChoiceAnswerEntity();
		answerEntity.setIdCard(req.getIdCard());
		answerEntity.setScreeningTime(req.getScreeningTime());
		answerEntity.setPhqScore(phqScore.toString());
		answerEntity.setPhqResult(phqResult);
		answerEntity.setGadScore(gadScore.toString());
		answerEntity.setGadResult(gadResult);
		// 新增筛查结果
		if(StringUtils.isNull(req.getAnswerId())) {
			answerEntity.setCreateTime(new Date());
			loseWisdomSingleChoiceAnswerDao.save(answerEntity);
		} else {
			// 更改筛查结果
			answerEntity.setUpdateTime(new Date());
			answerEntity.setId(req.getAnswerId());
			loseWisdomSingleChoiceAnswerDao.update(answerEntity);
		}

		//4、在【lose_wisdom_single_choice_answer】表中批量存入/批量修改答案明细
		List<LoseWisdomSingleChoiceAnswerDetailEntity> saveInfo = new ArrayList<>();
		LoseWisdomSingleChoiceAnswerDetailEntity detailEntity = new LoseWisdomSingleChoiceAnswerDetailEntity();

		// req.getAnswerId()为判断修改或新增的依据
		Integer answerId = StringUtils.isNull(req.getAnswerId()) ? answerEntity.getId() : req.getAnswerId();
		for(LoseWisdomSingleChoiceVo vo : answerList) {
			detailEntity = new LoseWisdomSingleChoiceAnswerDetailEntity(answerId, new Date(), new Date());
			BeanUtils.copyProperties(vo, detailEntity);
			saveInfo.add(detailEntity);
		}
		// 若是批量修改，则先批量删除，再批量新增
		if(StringUtils.isNotNull(req.getAnswerId())) {
			loseWisdomSingleChoiceAnswerDetailDao.deleteByAnswerId(req.getAnswerId());
		}
		loseWisdomSingleChoiceAnswerDetailDao.batchSaveAnswer(saveInfo);

	}

	/**
	 * 删除筛查问卷答案
	 * @param id
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteAnswer(Integer id) {
		loseWisdomSingleChoiceAnswerDetailDao.deleteByAnswerId(id);

		loseWisdomSingleChoiceAnswerDao.delete(id);
	}


	/**
	 * 计算各答案的分数
	 * @param answer 问题答案
	 * @return
	 */
	private Integer calculateAnswerScore(String answer) {
		int score;
		switch (answer) {
			case "A" :
				score = 0;
				break;
			case "B" :
				score = 1;
				break;
			case "C" :
				score = 2;
				break;
			case "D" :
				score = 3;
				break;
			default:
				score = 0;
				break;
		}
		return score;
	}

	/**
	 * 判断PHQ-9筛查结果
	 * @param score 分数
	 * @return
	 */
	private String decidePhqResult(Integer score) {
		if(score >= 0 && score <= 4) {
			return "没有抑郁症";
		} else if(score >= 5 && score <= 9) {
			return "可能有轻微抑郁症";
		} else if(score >= 10 && score <= 14) {
			return "可能有中度抑郁症";
		} else if(score >= 15 && score <= 19) {
			return "可能有中重度抑郁症";
		} else if(score >= 20 && score <= 27) {
			return "可能有重度抑郁症";
		} else {
			return "";
		}
	}

	/**
	 * 判断GAD-7筛查结果
	 * @param score 分数
	 * @return
	 */
	private String decideGadResult(Integer score) {
		if(score >= 0 && score <= 4) {
			return "没有GAD";
		} else if(score >= 5 && score <= 9) {
			return "轻度GAD";
		} else if(score >= 10 && score <= 14) {
			return "中度GAD";
		} else if(score >= 15 && score <= 21) {
			return "重度GAD";
		} else {
			return "";
		}
	}
}
